package qingyuan.com.listviewcheckbox;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private ListViewAdapter listViewAdapter;
    private List<String>strList=new ArrayList<>();
    private List<Boolean>boolList=new ArrayList<>();
    Boolean visflag=false;
    static String str[] ={"1苹果","2香蕉","3桔子","4猕猴桃","5李子","6甘蔗","7荔枝","8桃子","9香瓜","10葡萄","11哈密瓜","12桂圆","13龙眼","14","15","16","17","18","19","20"};
    {for (int i=0;i<str.length;i++){
        strList.add(str[i]);
        boolList.add(false);

    }}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listViewAdapter=new ListViewAdapter(this);

        lv= (ListView) findViewById(R.id.lv);
        lv.setAdapter(listViewAdapter);
        lv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (visflag){
                    ListViewAdapter.ViewHolder viewHolder = (ListViewAdapter.ViewHolder) view.getTag();
                    viewHolder.cb.toggle();
                    if(viewHolder.cb.isChecked())
                    {

                        boolList.set(position, true);
                    }else{
                        boolList.set(position, false);
                    }
                }
            }
        });
    }

    private class ListViewAdapter extends BaseAdapter{

        Context c;
        LayoutInflater mInflater ;

        public ListViewAdapter(Context c) {
            this.c = c;
            mInflater = getLayoutInflater();
        }
        @Override
        public int getCount()
        {
            return strList.size();
        }

        @Override
        public Object getItem(int position)
        {
            return strList.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }


        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent)
        {
            ViewHolder holder = null ;
            if(convertView == null)
            {

                holder = new ViewHolder();
                convertView  = mInflater.inflate(R.layout.list_item, null);
                holder.tv = (TextView)convertView.findViewById(R.id.textView);
                holder.cb = (CheckBox)convertView.findViewById(R.id.checkBox);
                convertView.setTag(holder);

            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(strList.get(position));
            holder.cb.setChecked(boolList.get(position));

            if(visflag)
            {
                holder.cb.setVisibility(View.VISIBLE);
            }
            else
            {
                holder.cb.setVisibility(View.INVISIBLE);
            }

            return convertView;
        }
        class ViewHolder
        {
            TextView tv;
            CheckBox cb;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,0,0,"批量处理");
        menu.add(0,1,0,"确定删除");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 0:
            {
                if(visflag)
                {
                    visflag = false;
                    for(int i=0; i<boolList.size();i++)
                    {
                        boolList.set(i, false);
                    }
                }
                else
                {
                    visflag = true;
                }
                this.listViewAdapter.notifyDataSetInvalidated();
                break;
            }
            case 1: //确定删除
            {
                if(boolList.size()>0)
                {
                    if(visflag)
                    {
                        for(int location=0; location<boolList.size(); )
                        {
                            if(boolList.get(location))
                            {
                                boolList.remove(location);
                                strList.remove(location);
                                continue;
                            }
                            location++;
                        }
                    }

                }
                this.listViewAdapter.notifyDataSetChanged();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }
}
